package com.itcast.donkey.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itcast.donkey.R;
import com.itcast.donkey.activity.DetailedTravelAreaInContentActivity;
import com.itcast.donkey.modles.DetailedTravel_area_Modles;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by lanouhn on 16/8/31.
 */
public class TravelAreaShowContentAdaper extends RecyclerView.Adapter<TravelAreaShowContentAdaper.TravelShowViewHolder> {

    private Context context;
    private DetailedTravel_area_Modles.AttractionTripTagsBean tripTagsBean;
    private String name;

    public TravelAreaShowContentAdaper(Context context, DetailedTravel_area_Modles.AttractionTripTagsBean tripTagsBean,String name) {
        this.context = context;
        this.tripTagsBean = tripTagsBean;
        this.name=name;
    }

    @Override
    public TravelShowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_show_content_detailed_travel_area, null);
        TravelShowViewHolder holder = new TravelShowViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TravelShowViewHolder holder, final int position) {

        holder.tvShowContent.setText(tripTagsBean.getAttraction_contents().get(position).getDescription());
        holder.tvShowContentUserName.setText("@" + tripTagsBean.getAttraction_contents().get(position).getTrip().getUser().getName());
        long times = tripTagsBean.getAttraction_contents().get(position).getTrip().getUpdated_at() * 1000l;
        Date date = new Date(times);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-dd");
        String time = simpleDateFormat.format(date);
        holder.tvShowContentDate.setText(time);
        if (!tripTagsBean.getAttraction_contents().get(position).getNotes().isEmpty()) {
            holder.horShowContent.setVisibility(View.VISIBLE);
            for (int i = 0; i < tripTagsBean.getAttraction_contents().get(position).getNotes().size(); i++) {

                LinearLayout ll = new LinearLayout(context);
                ll.setOrientation(LinearLayout.VERTICAL);
                ImageView image = new ImageView(context);
                TextView textView = new TextView(context);
                ImageOptions imageOptions = new ImageOptions.Builder()
                        .setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))//图片大小
                        .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
                        .setCrop(true)// 如果ImageView的大小不是定义为wrap_content, 不要crop.
                        .setImageScaleType(ImageView.ScaleType.CENTER_CROP)
                        .setLoadingDrawableId(R.mipmap.ic_launcher)//加载中默认显示图片
                        .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
                        .setUseMemCache(true)//缓存
                        .setIgnoreGif(false)//不忽略动态图
                        .build();
                x.image().bind(image, tripTagsBean.getAttraction_contents().get(position).getNotes().get(i).getPhoto_url(), imageOptions);
                textView.setText(tripTagsBean.getAttraction_contents().get(position).getNotes().get(i).getDescription());
                image.setScaleType(ImageView.ScaleType.FIT_XY);
                textView.setTextColor(context.getResources().getColor(R.color.travel_content));
                textView.setWidth(100);
                textView.setLines(1);
                holder.linImageShowContent.addView(image);
                holder.linTextviewName.addView(textView);
            }
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, DetailedTravelAreaInContentActivity.class);
                intent.putExtra("name",name);
                intent.putExtra("contents",tripTagsBean.getAttraction_contents().get(position));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return tripTagsBean.getAttraction_contents().size();
    }

    public class TravelShowViewHolder extends RecyclerView.ViewHolder {

        private TextView tvShowContent;
        private HorizontalScrollView horShowContent;
        private LinearLayout linImageShowContent;
        private LinearLayout linTextviewName;
        private TextView tvShowContentUserName;
        private TextView tvShowContentDate;

        public TravelShowViewHolder(View itemView) {
            super(itemView);
            tvShowContent = (TextView) itemView.findViewById(R.id.tv_show_content);
            horShowContent = (HorizontalScrollView) itemView.findViewById(R.id.hor_show_content);
            linImageShowContent = (LinearLayout) itemView.findViewById(R.id.lin_image_show_content);
            linTextviewName = (LinearLayout) itemView.findViewById(R.id.lin_textview_name);
            tvShowContentUserName = (TextView) itemView.findViewById(R.id.tv_show_content_user_name);
            tvShowContentDate = (TextView) itemView.findViewById(R.id.tv_show_content_date);
        }
    }
}
